﻿isperp.createListSelector = function(options) {
	var o = $.extend({}, {
		id : "",
		autocompleteUrl : undefined,
		autocompleteDisplay : undefined,
		valueControlId : undefined,
		valueColumn : undefined,
		displayColumn : undefined,
		afterSelected : undefined,
		listUrl : undefined,
		urlExtScript : undefined,
		dialogWidth : undefined,
		dialogHeight : undefined,
		reloadOnOpen : false
 
	}, options);

	var self = this, $element = $(this), init = function() {
		$("#" + o.id + "Dialog").dialog({
			width : o.dialogWidth,
			height : o.dialogHeight,
			modal : true,
			autoOpen : false,
			resizable : false,
			beforeClose : function(event, ui) {
				$("#" + o.id).removeClass("selector-hover");
			},
			buttons : [ {
				text : "确定",
				"class" : 'submit nui-mainbtn',
				click : function() {
					var row = isperp.getWindow(o.id + "IFrame").getSelectedValue();
					if ($result.setSelected(row) == true) {
						$(this).dialog("close");
					}
				}
			}, {
				text : "关闭",
				click : function() {
					$(this).dialog("close");
				}
			} ]

		});

		if (o.autocompleteUrl) {
			var uri = o.autocompleteUrl;

			if (uri.indexOf("?") > 0) {
				uri = encodeURI(uri + "&" + $("#" + o.id + "Param").text());
			} else {
				uri = encodeURI(uri + "?" + $("#" + o.id + "Param").text());
			}

			$("#" + o.id).autocomplete({
				source : function(request, response) {
					$.ajax({
						url : uri + "&" + (o.urlExtScript ? o.urlExtScript() : ""),
						dataType : "json",
						data : {
							term : request.term

						},
						success : function(data) {
							response($.map(data.content || data, function(item) {
								return {
									data : item,
									value : item[o.displayColumn]
								}
							}));
						}
					});
				},
				minLength : 1,
				change : function(event, ui) {
					if (!ui.item) {
						if (o.afterSelectEmpty) {
							o.afterSelectEmpty();
						}
					}
				},
				select : function(event, ui) {
					var ids = ui.item.data[o.valueColumn];
					var names = ui.item.data[o.displayColumn];
					var rows=new Array();
					rows[0]=ui.item.data;
					$("#" + o.valueControlId).val(ids);
					$("#" + o.id).val(names);
					if (o.afterSelected) {
						o.afterSelected(ids, names,rows);
					}
				}
			}).data("ui-autocomplete")._renderItem = function(ul, item) {
			 
				return $("<li><a href='javascript:void(0);' data-vid='" + item.data[o.valueColumn] + "'>" + o.autocompleteDisplay(item,o) + "</a></li>").appendTo(ul);
			};

		}

	}, $result = {
		setParam : function(param) {
			$("#" + o.id + "Param").text(param)
		},
		onClick : function() {
			var uri = o.listUrl;
			if (o.urlExtScript)
				uri += o.urlExtScript();
			if (uri.indexOf("?") > 0) {
				uri = encodeURIComponent(uri + "&" + $("#" + o.id + "Param").text())
			} else {
				uri = encodeURIComponent(uri + "?" + $("#" + o.id + "Param").text())
			}

			var url = $("#" + o.id + "IFrame").attr("src");

			if (o.reloadOnOpen) {
				$("#" + o.id + "IFrame").attr("src",  $("#" + o.id + "Url").text() + uri + "&colNames=" + encodeURIComponent($("#" + o.id + "ColNames").text()));
			} else if (url == null || url == "" || url == undefined) {
				$("#" + o.id + "IFrame").attr("src", $("#" + o.id + "Url").text() + uri + "&colNames=" + encodeURIComponent($("#" + o.id + "ColNames").text()));
			}
			$("#" + o.id + "Dialog").dialog("open");

			$("#" + o.id).addClass("selector-hover");
			return false;
		},
		closeDialog : function() {
			$("#" + o.id + "Dialog").dialog("close");
		},
		setSelected : function(rows) {
			if (rows == undefined || rows.length == 0 || rows[0][o.valueColumn] == undefined) {
				isperp.alert("请选择一条数据！");
				return false;
			}
			var ids = rows[0][o.valueColumn];
			var names = rows[0][o.displayColumn];
			for ( var i = 1; i < rows.length; i++) {
				var item = rows[i];
				ids = ids + "," + item["${valueColumn}"];
				names = names + "," + item["${displayColumn}"];
			}
			if (o.afterSelected) {
				o.afterSelected(ids, names, rows);
			}

			$("#" + o.valueControlId).val(ids);
			$("#" + o.id).val(names);
			return true;
		}
	};
	init();
	return $result;
};
